package io.renren.data;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.TypeReference;
import io.renren.RenrenApplication;
import io.renren.common.to.processon.MindTo;
import io.renren.common.to.processon.NodeChildrenTo;
import io.renren.common.utils.JsonUtils;
import io.renren.common.vo.ProcessOnMindVo;
import io.renren.modules.avanotes.dao.ClassifySubjectDao;
import io.renren.modules.avanotes.dao.SpecialSubjectDao;
import io.renren.modules.avanotes.entity.ClassifySubjectEntity;
import io.renren.modules.avanotes.entity.SpecialSubjectEntity;
import io.renren.modules.avanotes.service.ClassifySubjectService;
import io.renren.modules.avanotes.service.SpecialSubjectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: xiaomi
 * @date: 2022/2/19
 * @description: 导图数据导入
 * https://www.processon.com/view/5e86b713e4b0bf3ebcf4e376
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RenrenApplication.class)
@Slf4j
public class MindClassImport {

    private Map<String, Integer> map = new HashMap<>();

    @Autowired
    ClassifySubjectService csService;

    @Autowired
    ClassifySubjectDao csDao;

    @Autowired
    SpecialSubjectDao ssDao;

    @Autowired
    SpecialSubjectService ssService;


    /**
     * 将 ProcessOn 的 思维导图导入到 MindBook
     *
     * @throws Exception
     */
    @Test
    public void importProcessOnCommon2Special() throws Exception {
        int mindBookId = 3;
        String filePath = "D:\\Projects\\Javas\\avanotes-backend\\files\\PLM-WindChill.txt";
        filePath = "D:\\Projects\\IdeaProjects\\Projections\\AvaNotes\\avanotes-backend\\files\\PLM-WindChill.txt";

        importSelections2Special(mindBookId, filePath, true);
    }


    @Test
    public void importAoBing2Special() throws Exception {
        String filePath = "D:\\Projects\\IdeaProjects\\Projections\\AvaNotes\\avanotes-backend\\files\\aobing-mind.txt";
        importSelections2Special(1, filePath, false);
    }

    @Test
    public void importTuling2Special() throws Exception {
        int mindBookId = 2;
        String filePath = "D:\\Projects\\Javas\\avanotes-backend\\files\\大厂认证标准.txt";
        filePath = "D:\\Projects\\IdeaProjects\\Projections\\AvaNotes\\avanotes-backend\\files\\大厂认证标准.txt";
        importSelections2Special(mindBookId, filePath, false);
    }


    /**
     * 根据 mindBookId 和 本地路径导入到 专题表
     *
     * @param mindBookId
     * @param filePath
     * @throws Exception
     */
    private void importSelections2Special(int mindBookId, String filePath, boolean needPreDeal) throws Exception {
        String str = FileUtil.readString(new File(filePath), Charset.defaultCharset());
        if (needPreDeal) {
            //需要预处理
            ProcessOnMindVo processOnMindVo = JsonUtils.string2Obj(str, new TypeReference<ProcessOnMindVo>() {
            });
            //替换\
            str = processOnMindVo.getDef().replaceAll("\\\\", "");
        }
        MindTo mindTo = JsonUtils.string2Obj(str, new TypeReference<MindTo>() {
        });
        SpecialSubjectEntity entity = new SpecialSubjectEntity(mindTo.getTitle(), mindBookId, 0);
        ssDao.insert(entity);
        map.put(mindTo.getId(), entity.getId());

        insertByChildren(mindTo.getChildren(), mindBookId);
    }

    private void insertByChildren(List<NodeChildrenTo> children, int mindBookId) {
        if (CollectionUtils.isEmpty(children)) {
            return;
        }
        //对非空进行导入
        for (NodeChildrenTo child : children) {
            if (StringUtils.isBlank(child.getId())) {
                log.info(child.toString());
            } else {
//                list.add(new ClassifySubjectEntity(child.getId(), child.getTitle(), 1, child.getParent()));
                Integer pid = map.get(child.getParent());
                if (pid == null) {
                    //没有找到PID
                    log.error("没有找到PID,{}", child.getTitle());
                } else {
                    SpecialSubjectEntity entity = new SpecialSubjectEntity(child.getTitle(), mindBookId, pid);
                    ssDao.insert(entity);
                    map.put(child.getId(), entity.getId());
                }
            }

            insertByChildren(child.getChildren(), mindBookId);
        }
    }

    /**
     * 还是换成自增ID 好了
     *
     * @throws Exception
     */
    @Test
    public void importAoBing() throws Exception {
        String filePath = "D:\\Projects\\IdeaProjects\\Projections\\AvaNotes\\avanotes-backend\\files\\aobing-mind.txt";
        String str = FileUtil.readString(new File(filePath), Charset.defaultCharset());
        MindTo mindTo = JsonUtils.string2Obj(str, new TypeReference<MindTo>() {
        });

        List<ClassifySubjectEntity> list = new ArrayList<>();
        list.add(new ClassifySubjectEntity(mindTo.getId(), mindTo.getTitle(), 1, ""));
        genListByChildren(mindTo.getChildren(), list);

//        csService.saveBatch(list);
        for (ClassifySubjectEntity entity : list) {
            try {
                csService.save(entity);
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
    }

    private void genListByChildren(List<NodeChildrenTo> children, List<ClassifySubjectEntity> list) {
        if (CollectionUtils.isEmpty(children)) {
            return;
        }
        //对非空进行导入
        for (NodeChildrenTo child : children) {
            if (StringUtils.isBlank(child.getId())) {
                log.info(child.toString());
            } else {
                list.add(new ClassifySubjectEntity(child.getId(), child.getTitle(), 1, child.getParent()));
            }


            genListByChildren(child.getChildren(), list);
        }
    }

//    public static void main(String[] args) throws Exception {
//        MindClassImport mindClassImport = new MindClassImport();
//        mindClassImport.importAoBing();
//    }
}

